package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.fanwei.sdk.activity.HandlerCallback;
import com.fanwei.sdk.api.PaySdk;
import com.fanwei.sdk.bean.PayParam;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * 充值
 * 
 * @author zk
 *
 */
public class RecargeActivity extends BaseActivity implements OnCheckedChangeListener, TextWatcher, android.widget.CompoundButton.OnCheckedChangeListener {

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字
	
	private Activity context;

	/** 选择金额 */
	private RadioGroup recarge_amount_options;
	private RadioButton btn_0, btn_1, btn_2, btn_3;
	/** 输入金额 */
	private EditText recarge_input;
	/** 汇潮支付 */
	private ToggleButton recarge_huic;
	/** 支付宝支付 */
	private ToggleButton recarge_zhb;
	/** 应付金额 */
	private TextView recarge_amount;
	/** 确认充值 */
	private Button recarge_btn;
	/**
	 * 选择支付方式：0表示汇潮支付，1表示支付宝支付
	 */
	private int payWay = 0;

	@Override
	public int layoutResID() {
		return R.layout.activity_recarge;
	}

	@Override
	public void findView() {
		findTitleIds();
		context = this;
		recarge_amount_options = V.f(this, R.id.recarge_amount_options);
		btn_0 = V.f(this, R.id.btn_0);
		btn_1 = V.f(this, R.id.btn_1);
		btn_2 = V.f(this, R.id.btn_2);
		btn_3 = V.f(this, R.id.btn_3);
		recarge_input = V.f(this, R.id.recarge_input);
		recarge_huic = V.f(this, R.id.recarge_huic);
		recarge_zhb = V.f(this, R.id.recarge_zhb);
		recarge_amount = V.f(this, R.id.recarge_amount);
		recarge_btn = V.f(this, R.id.recarge_btn);
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("充值");
		base_right_tv.setVisibility(View.GONE);

	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		recarge_btn.setOnClickListener(this);
		recarge_amount_options.setOnCheckedChangeListener(this);
		recarge_input.addTextChangedListener(this);
		recarge_input.setOnClickListener(this);
		
		recarge_huic.setOnCheckedChangeListener(this);
		recarge_zhb.setOnCheckedChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.recarge_btn:// 确认充值
			String amount_str = recarge_amount.getText().toString().trim();
			//showToast(amount_str);
			jubaoPay(amount_str);
			break;
		case R.id.recarge_input:
			btn_0.setChecked(false);
			btn_1.setChecked(false);
			btn_2.setChecked(false);
			btn_3.setChecked(false);
			break;
		}
	}
	
	private void jubaoPay(String amount) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		try {
			json.put("uid",PrefUtils.getString(context, "userId", ""));
			json.put("money", amount);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.configCookieStore(Values.cookieStore);
		utils.send(HttpMethod.POST, OApi.Public_jubaoCz, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject js = new JSONObject(result);
					if(js.getInt("status") == 1){
						JSONObject data = js.getJSONObject("data");
						//String goodsname = data.getString("goodsname");
						String payid = data.getString("payid");
						String amount = data.getString("money");
						String playerid = data.getString("playerid");
						
						PayParam payParam = new PayParam();
						payParam.setAmount(amount);
						payParam.setGoodsname("充值");
						payParam.setPlayerid(playerid);
						payParam.setPayid(payid);
						
						PaySdk.startPay(context, payParam, new HandlerCallback() {
							
							@Override
							public void processResult(int arg0, String arg1) {
								// TODO Auto-generated method stub
								/*String result = arg0 + arg1;
								Toast.makeText(context, result, 0).show();*/
								/*Toast.makeText(context, "[resultCode=" + arg0 + "][result=" + arg1 + "]",
										Toast.LENGTH_LONG).show();*/
								Toast.makeText(context, arg1, 0).show();
									switch (arg0) {
									case 0:
										context.finish();
										/*for (int i = 0; i < carts.size(); i++) {
											cartProvider.delete(carts.get(i));
										}
										MainActivity.badgeView.decrement(Values.idList.size());
										Values.idList.clear();
										Intent in = new Intent(context, PurchaseRecordActivity.class);
										in.putExtra("type", 1);
										startActivity(in);
										context.finish();*/
										break;
									default:
										break;
									}
							}
						});
						
					}else{
						Toast.makeText(context, "支付遇到问题，请联系客服人员！", 0).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	// RadioGroup监听，选择充值给定的金额
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == btn_0.getId()) {
			recarge_amount.setText("50");
			recarge_input.setText("");
		} else if (checkedId == btn_1.getId()) {
			recarge_amount.setText("100");
			recarge_input.setText("");
		} else if (checkedId == btn_2.getId()) {
			recarge_amount.setText("200");
			recarge_input.setText("");
		} else if (checkedId == btn_3.getId()) {
			recarge_amount.setText("500");
			recarge_input.setText("");
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	//recarge_input输入的金额监听
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (s.length() > 0) {
			recarge_amount.setText("");
			String amounts = recarge_input.getText().toString().trim();
			recarge_amount.setText(amounts);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.recarge_huic://汇潮支付
			if (isChecked) {
				payWay = 0;
			}
			if (payWay == 0){
				recarge_zhb.setChecked(false);
			}
			break;
		case R.id.recarge_zhb://支付宝支付
			if (isChecked) {
				payWay = 1;
			}
			if (payWay == 1){
				recarge_huic.setChecked(false);
			}
			break;
		}
	}

}
